const middleware1 = (req, res, next) => {
  console.log('middleware1 start')
  // 所有的中间件都应返回一个Promise对象
  // Promise.resolve()方法接收中间件返回的Promise对象，供下层中间件异步控制
  return next().then((result) => {
    console.log('middleware1 end')
    console.log(result)
  })
}

// async函数自动返回Promise对象
const middleware2 = async (req, res, next) => {
  console.log('middleware2 start')
  // await new Promise(resolve => {
  //   setTimeout(() => resolve(), 1000)
  // })
  let result = await next()
  console.log('middleware2 end')
  return result
}

const middleware3 = async (req, res, next) => {
  console.log('middleware3 start')
  await next()
  console.log('middleware3 end')
  return 200
}

// 中间件数组
const middlewares = [middleware1, middleware2, middleware3]

function run (req, res, middlewares) {
  const next = () => {
    const middleware = middlewares.shift()
    if (middleware) {
      // 将middleware(req, res, next)包装为Promise对象
      return Promise.resolve(middleware(req, res, next))
    }
  }
  next().then((result) => {
    console.log(result)
  })
}

run(middlewares) // 模拟一次请求发起